﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Linq.SqlClient;
using FilstartupsREST.Models;

namespace FilstartupsREST.FILSTARTUPSService.rest.Controllers
{
    public class PostController : Controller
    {
        //
        // GET: /Post/
        FilStartUpsEntities cms_db = new FilStartUpsEntities();

        /*
         * GET POST DETAILS
         * FOR SPECIFIC POST
         * ID & POSTTITLE OPTION
         * */
        [HttpGet]
        public JsonResult GetPostDetails(string id)
        {
            int postid_ = int.Parse(id);

            var post = (from p in cms_db.Posts
                        where p.postidPK.Equals(postid_)
                        select new LatestPost
                        {
                            catidFK = p.catidFK,
                            postdescription = p.postdescription,
                            postidPK = p.postidPK,
                            posttitle = p.posttitle

                        }).SingleOrDefault();

            /** TEMP HACK 
                16= EVENTS
                31= NEWS   **/

            //var post_ = cms_db.Posts.OrderByDescending(p => p.postidPK)
            //                    .Where(p=>p.catidFK != 16 && p.catidFK != 31).ToList().Take(5); 

            // TEST VALIDATION FOR HACK POSTIDPK in URL
            //foreach (var postarticle in post_)
            //{
            //    if (post.postidPK == postarticle.postidPK)
            //    {  
            if (post != null)
            {
                return Json(new { data = post, success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
            }
            //    }

            //}


        }

        [HttpGet]
        public JsonResult GetArticleDetails(string id)
        {
            int postid_ = int.Parse(id);

            var post = (from p in cms_db.Posts
                        where p.postidPK.Equals(postid_)
                        select new LatestPost
                        {
                            postdescription = p.postdescription,
                            postidPK = p.postidPK,
                            posttitle = p.posttitle,
                            catidFK = p.catidFK

                        }).SingleOrDefault();

            /** TEMP HACK 
                16= EVENTS
                31= NEWS   **/

            var post_ = cms_db.Posts.OrderByDescending(p => p.postidPK)
                                .Where(p => p.catidFK != 16 && p.catidFK != 31).ToList().Take(5);

            // TEST VALIDATION FOR HACK POSTIDPK in URL
            foreach (var postarticle in post_)
            {
                if (post.postidPK == postarticle.postidPK)
                {
                    return Json(new { data = post, success = true }, JsonRequestBehavior.AllowGet);
                }

            }

            return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);

        }

        /*
         * GET ARCHIEVES POST
         * GET ID 
         * CATEGORYID_FK
         * */
        [HttpGet]
        public JsonResult GetPostArchieves(string id)
        {
            int catid_ = int.Parse(id);
            var post = (from p in cms_db.Posts
                        where p.routingidFK.Equals(2)
                         && p.catidFK.Equals(catid_)
                        select new PostDetail
                        {
                            datecreated = p.datecreated,
                            posdescription = p.postdescription,
                            postidPK = p.postidPK,
                            posttitle = p.posttitle
                        }).ToList();

            cms_db.Posts.Where(p => p.routingidFK == 2 && p.catidFK == catid_).ToList();
            if (post != null)
            {
                return Json(new { data = post, success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
            }
        }
        /*
         *LATEST ARTICLES
         * */
        [HttpGet]
        public JsonResult GetLatestArticles()
        {
            var latestpost = (from p in cms_db.Posts
                              where p.routingidFK.Equals(2)
                              && p.featuredpost.Equals(false)
                              orderby p.postidPK descending
                              select new LatestPost
                                   {
                                       postidPK = p.postidPK,
                                       datecreated = p.datecreated,
                                       posttitle = p.posttitle,
                                       postdescription = p.postdescription
                                   }).Take(5).ToList();



            if (latestpost.Count() > 0)
            {
                return Json(new { data = latestpost, success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
            }
        }
        [HttpGet]
        public JsonResult GetEventDetail(string id)
        {
            int eventid = int.Parse(id);
            var events_ = (from e in cms_db.Events
                           where e.routingidFK.Equals(2)
                           && e.eventidPK.Equals(eventid)

                           select new EventPost
                           {
                               eventdescription = e.eventdescription,
                               eventidPK = e.eventidPK,
                               eventtitle = e.eventtitle,
                               startdate = e.eventdatestart,
                               enddate = e.eventdateend
                           }).SingleOrDefault();

            if (events_ != null)
            {
                return Json(new { data = events_, success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
            }
        }
        /*
         * Get Event Archives
         * */
        public JsonResult GetEventArchives()
        {
            var eventarchives = cms_db.Events.OrderByDescending(e => e.eventidPK).ToList();
            if (eventarchives.Count() > 0)
            {
                return Json(new { data = eventarchives }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
            }
        }

        /*
         * EVENTS DETAILS
         * TAKE(5)
         * CATIDFK = EVENTS
         * */

        [HttpGet]
        public JsonResult GetLatestEvents()
        {

            var events = (from e in cms_db.Events
                          where e.routingidFK.Equals(2)
                          orderby e.eventidPK ascending
                          select new EventPost
                          {
                              eventdescription = e.eventdescription,
                              eventidPK = e.eventidPK,
                              eventtitle = e.eventtitle,
                              startdate = e.eventdatestart,
                              enddate = e.eventdateend
                          }).Take(5).ToList();

            //cms_db.Posts.Where(p=>p.catidFK == 16 && p.routingidFK.Equals(2) ).Take(5).ToList();

            if (events != null)
            {
                return Json(new { data = events, success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
            }
        }

        /*
         * NEWS DETAILS
         * TAKE(5)
         * CATIDFK = NEWS
         * */
        [HttpGet]
        public JsonResult GetNewsDetails()
        {
            var news = cms_db.Posts.Where(p => p.catidFK.Equals(3) && p.routingidFK.Equals(2)).Take(5).ToList();
            if (news.Count() > 0)
            {
                return Json(new { data = news, success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
            }
        }

        // Sample Paging
        [HttpGet]
        public JsonResult GetArticleArchieves(string id)
        {

            int catidFK_ = int.Parse(id);

            //var archieves = cms_db.Posts.Where(p => p.catidFK.Equals(catidFK_))
            //                                        .OrderBy(p=>p.postidPK).Skip(page_*5).Take(10).ToList();

            var archieves = (from p in cms_db.Posts
                             where p.catidFK.Equals(catidFK_)
                             orderby p.postidPK
                             select new LatestPost
                             {
                                 postdescription = p.postdescription,
                                 posttitle = p.posttitle,
                                 postidPK = p.postidPK,
                                 catidFK = p.catidFK,
                                 datecreated = p.datecreated
                             }).ToList().Take(50);

            if (archieves != null && archieves.Count() > 0)
            {
                return Json(new { archieves, success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { archieves = string.Empty }, JsonRequestBehavior.AllowGet);
            }
        }

        //Get Featured Post
        [HttpGet]
        public JsonResult GetFeaturedPost()
        {
            var featured = (from p in cms_db.Posts
                            where p.routingidFK.Equals(2)
                              && p.featuredpost.Equals(true)
                            orderby p.postidPK descending
                            select new FeaturedPost
                            {
                                postdescription = p.postdescription,
                                postidPK = p.postidPK,
                                posttitle = p.posttitle

                            }).ToList().Take(4).OrderByDescending(p => p.postidPK);

            if (featured != null || featured.Count() > 0)
            {
                return Json(new { featured, success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { featured = string.Empty }, JsonRequestBehavior.AllowGet);
            }

        }
        [HttpGet]
        public JsonResult GetSearchPost(string name)
        {
            var searchquery = cms_db.Posts.Where(p => p.posttitle.Contains(name.Trim())
                                                && p.routingidFK.Equals(2)).ToList();
            if (searchquery.Count() > 0)
            {
                return Json(new { searchquery }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { searchquery = string.Empty }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet]
        public JsonResult GetPostImage(string id)
        {
            int postid = int.Parse(id);

            var post = (from p in cms_db.Posts
                        where p.postidPK.Equals(postid)
                        && p.routingidFK.Equals(2)
                        select new PostImage
                        {
                            image = p.image,
                            postidPK = p.postidPK,
                            imagetype = p.imagetype,
                            filename = p.filename
                        }).SingleOrDefault();

            if (post.image != null)
            {
                byte[] api_image = post.image;
                string base64string_ = Convert.ToBase64String(api_image);

                //Test
                PostImage_String post_ = new PostImage_String()
                {
                    base64string = base64string_,
                    filename = post.filename,
                    imagetype = post.imagetype,
                    postidPK = post.postidPK
                };


                // post.imagetype = postimage.ContentType;
                //post.filename = postimage.FileName;
                //int lenght = postimage.ContentLength;
                //byte[] buffer = new byte[lenght];
                //postimage.InputStream.Read(buffer, 0, lenght);
                //post.image = buffer;



                //postidPK = post.postidPK,
                //    image = base64string,
                return Json(new
                {
                    data = post_,
                    success = true
                }, JsonRequestBehavior.AllowGet);


                // return File(api_image, post.imagetype,post.filename);
            }
            else
            {
                return Json(new { data = string.Empty }, JsonRequestBehavior.AllowGet);
            }

        }

        #region Get LastestArticles in each category
        /* QUERY OF ACTIVE ARTICLES
         * TEST LATEST ARTCLES
         * Applicable
         * */
        [HttpGet]
        public JsonResult Get()
        {

            var catergory = cms_db.Categories.Where(c => c.routingidFK.Equals(1)).ToList();
            var TotalSearch = new TotalSearch();

            int[] catidPK = { };
            foreach (var item in catergory)
            {
                TotalSearch.post_ = cms_db.Posts.Where(p => p.catidFK == item.catidPK && p.routingidFK == 1).ToList();
                TotalSearch.post_1 = TotalSearch.post_;
            }

            // 
            //  TotalSearch.post_1 = cms_db.Posts.Where(p => p.postidPK == 2).SingleOrDefault();

            return Json(new { data = TotalSearch, success = true }, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
